# -*- coding: UTF-8 -*-
# @Time : 2021-01-05 20:03
# @File : ExcelData.py
# 1、初始化测试用例，分别初始化4个sheet对象
# 2、分别定义读取4个sheet数据方法
# 3、获取全部真实有效测试用例
# 4、根据tc_id获取相应的列表数据
# 5、判断是否运行，y 获取执行用例
# 6、获取运行测试用例列表
from utils.ExcelUtil import ExcelReader
from utils.LogUtil import my_log
from data.ExcelConfig import ExcelSheet, TestCases, TestSteps, CaseData, Elements


class Data:
    def __init__(self, case_file):
        self.log = my_log('ExcelData')
        self.reader_cases = ExcelReader(case_file, ExcelSheet.test_case)
        self.reader_steps = ExcelReader(case_file, ExcelSheet.test_step)
        self.reader_data = ExcelReader(case_file, ExcelSheet.test_data)
        self.reader_elements = ExcelReader(case_file, ExcelSheet.test_ele)

# 2、分别定义读取4个sheet数据方法
    def get_cases_sheet(self):
        """
        获取测试用例TestCases的sheet
        :return:
        """
        return self.reader_cases.data()

    def get_steps_sheet(self):
        """
        获取测试用例TestSteps的sheet
        :return:
        """
        return self.reader_steps.data()

    def get_data_sheet(self):
        """
        获取测试用例CaseData的sheet
        :return:
        """
        return self.reader_data.data()

    def get_ele_sheet(self):
        """
        获取测试用例Elements的sheet
        :return:
        """
        return self.reader_elements.data()

# 3、获取全部真实有效测试用例
    def get_cases_all(self):
        """
        获取TestCases测试用例全部内容，过滤空的内容
        :return:
        """
        data_list = self.get_cases_sheet()
        res = self.get_no_empty(data_list, TestCases.CASES_TC_ID)
        return res

    def get_steps_all(self):
        """
        获取TestSteps测试用例全部内容，过滤空的内容
        :return:
        """
        data_list = self.get_steps_sheet()
        res = self.get_no_empty(data_list, TestSteps.STEP_TC_ID)
        return res

    def get_data_all(self):
        """
        获取CaseData测试用例全部内容，过滤空的内容
        :return:
        """
        data_list = self.get_data_sheet()
        res = self.get_no_empty(data_list, CaseData.DATA_TC_ID)
        return res

    def get_ele_all(self):
        """
        获取Elements测试用例全部内容，过滤空的内容
        :return:
        """
        data_list = self.get_ele_sheet()
        res = self.get_no_empty(data_list, Elements.ELE_TC_ID)
        return res

    def get_no_empty(self, data_list, condition):
        """
        根据condition获取非空数据
        :param data_list:
        :param condition:
        :return:
        """
        res = []
        for data in data_list:
            if data[condition] != '':
                res.append(data)
        return res

# 4、根据tc_id获取相应的列表数据
    def get_data_by_tc_id(self, tc_id):
        """
        根据cases表tc_id获取CaseData相应列表数据
        :param tc_id:
        :return:
        """
        data_all = self.get_data_all()
        # 根据tc_id获取数据
        """
        data_all_tc = []
        for data in data_all:
            if data['TC_ID'] == tc_id:
                data_all_tc.append(data)
                """
        data_all_tc = self.get_by_tc_id(data_all, tc_id)
        return data_all_tc

    def get_by_tc_id(self, data_all, tc_id):
        """
        TC_ID的对比判断封装
        :param data_all:
        :param tc_id:
        :return:
        """
        data_all_tc = []
        for data in data_all:
            if data[CaseData.DATA_TC_ID] == tc_id:
                data_all_tc.append(data)
        return data_all_tc

    def get_steps_by_tc_id(self, tc_id):
        """
        根据cases表tc_id获取TestSteps相应列表数据
        :param tc_id:
        :return:
        """
        data_all = self.get_steps_all()
        steps_all_tc = self.get_by_tc_id(data_all, tc_id)
        return steps_all_tc

    def get_ele_by_tc_id(self, tc_id):
        """
        根据cases表tc_id获取elements相应列表数据
        :param tc_id:
        :return:
        """
        data_all = self.get_ele_all()
        ele_all_tc = self.get_by_tc_id(data_all, tc_id)
        return ele_all_tc

    def get_ele_by_element(self, tc_id, element_name):
        """
        #获取element_name和Elements表ELE_NAME元素名称相同的数据

        get_ele_by_tc_id(tc_id) 根据tc_id获取elements表相应数据
        ele[Elements.ELE_NAME]  获取Elements表ELE_NAME元素名称
        :param tc_id:
        :param element_name:
        :return:
        """
        elements = self.get_ele_by_tc_id(tc_id)
        res = None
        for ele in elements:
            if str(ele[Elements.ELE_NAME]) == str(element_name):
                res = ele
        return res

# 5、判断是否运行，y 获取执行用例
    def get_run_cases(self):
        """
        判断TestCases中 Y 用例
        :return:
        """
        run_list = self.get_cases_all()
        run_cases_list = []
        for line in run_list:
            if str(line[TestCases.CASE_IS_RUN]).lower() == 'y':
                run_cases_list.append(line)
        return run_cases_list

    def get_run_data(self, tc_id):
        """
        判断CaseData中 Y 和 TC_ID 用例,
        :return:
        """
        run_list = self.get_data_all()
        run_data_list = []
        for line in run_list:
            if str(line[CaseData.DATA_IS_RUN]).lower() == 'y' and tc_id in line[CaseData.DATA_TC_ID]:
                run_data_list.append(line)
        return run_data_list

# 6、获取运行测试用例列表
    def run_list(self):
        cases = self.get_run_cases()
        self.log.debug('获取TestCase表测试个数{}。数据内容{}'.format(len(cases), cases))
        data_list = list()
        for case in cases:
            tc_id = case[CaseData.DATA_TC_ID]
            tmp_data_list = self.get_run_data(tc_id)
            # 1、增加allure备注，描述
            desc = case[TestCases.CASES_DESC]  # 描述
            note = case[TestCases.CASE_NOTE]  # 备注
            for data in tmp_data_list:
                data.update({TestCases.CASE_NOTE: note})
                data.update({TestCases.CASES_DESC: desc})
            data_list.extend(tmp_data_list)
            # data_list.extend(self.get_run_data(tc_id))
        self.log.debug('获取CaseData运行测试个数{}。数据内容{}'.format(len(data_list), data_list))
        return data_list


if __name__ == '__main__':
    res_data = Data('../data/data.xls')
    # print(res_data.get_cases_all())
    # print(res_data.get_steps_all())
    # print(res_data.get_data_all())
    # print(res_data.get_ele_all())
    # print(res_data.get_data_by_tc_id('TC_Login'))
    # print(res_data.get_steps_by_tc_id('TC_Login'))
    # print(res_data.get_ele_by_tc_id('TC_Login'))
    # print(res_data.get_run_cases())
    # print(res_data.get_run_data('TC_Login'))
    print(res_data.run_list())
    # log = my_log('EXCELDATA')
    # log.debug(res_data.run_list())

